<?php
# 文件名称:router.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
class C_router {
	var $config;	
	var $routes 		= array();
	var $error_routes	= array();
	var $class			= '';
	var $method			= '';
	var $directory		= '';
	var $uri_protocol 	= 'auto';
	var $default_controller;
	var $controller_trigger	= 'c';
	var $function_trigger = 'm';
	var $directory_trigger = 'd';
	
	function C_router()
	{
		$SYS = &get_instance();
		$this->config =&$SYS->config;
		$this->uri =&$SYS->uri;
		$this->_set_routing();
	}
	
	
	/**
	 * 设置路径映射
	 *
	 */
	function _set_routing()
	{
		// 采用请求方式路径
		if (isset($_GET[$this->controller_trigger]))
		{
			
			// 控制器有子目录时
			if (is_dir(PATH_BASE.DS.'controllers'.DS.$_GET[$this->controller_trigger]))
			{			
				$this->set_directory($_GET[$this->controller_trigger]);
			}
			$this->set_class(trim($this->uri->_filter_uri($_GET[$this->controller_trigger])));
			
			if (isset($_GET[$this->function_trigger]))
			{
				$this->set_method(trim($this->uri->_filter_uri($_GET[$this->function_trigger])));
			}			
			return;
		}
		if(defined( 'ISADMIN' ))
			$this->default_controller = "admin";
		else	
			$this->default_controller = "site";
		
		// 解析URL
		
		$this->uri->_fetch_uri_string();
		
		// 如果为空则调用默认控制器
		if ($this->uri->uri_string == '')
		{
			if ($this->default_controller === FALSE)
			{
				show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
			}
			
			if (strpos($this->default_controller, '/') !== FALSE)
			{
				$x = explode('/', $this->default_controller);
				$this->set_class(end($x));
				$this->set_method('');
				$this->_set_request($x);
			}
			else
			{
				$this->set_class($this->default_controller);
				$this->set_method('');
				$this->_set_request(array($this->default_controller, ''));
			}
			
			// re-index the routed segments array so it starts with 1 rather than 0
			$this->uri->_reindex_segments();			
			return;
		}
		
		// 将分词合成数组
		$this->uri->_explode_segments();
		
		$this->_set_request($this->uri->segments);
		$this->uri->_reindex_segments();
		
		if(!defined( 'ISADMIN' ))
		{
			$this->class = "site";
		}
	}
	
	/**
	 * 处理路由请求
	 *
	 */
	function _set_request($segments = array())
	{
		//与C_request接口
		$_GET = $this->uri->uri_to_assoc($this->uri->total_segments());
		$segments = $this->_validate_request($segments);
		
		if (count($segments) == 0)
		{
			return;
		}
						
		$this->set_class($segments[0]);
		
		if (isset($segments[1]))
		{
			// 标准的函数方法请求
			$this->set_method($segments[1]);
		}
		else
		{
			// 默认方法
			$segments[1] = '';
		}
		
		// 更新分词
		$this->uri->rsegments = $segments;
	}
	
	
	function _validate_request($segments)
	{
		
		if (!defined( 'ISADMIN' ) || defined( 'ISADMIN' ) && file_exists(PATH_BASE.DS.'controllers'.DS.$segments[0].EXT))
		{
			return $segments;
		}
		// 控制器有子目录时
		if (is_dir(PATH_BASE.DS.'controllers'.DS.$segments[0]))
		{		
			// 设置路径并在数组分词中移除
			$this->set_directory($segments[0]);
			$segments = array_slice($segments, 1);
			if (count($segments) > 0)
			{
				if ( defined( 'ISADMIN' ) && ! file_exists(PATH_BASE.DS.'controllers'.DS.$this->fetch_directory().$segments[0].EXT))
				{
					show_404($this->fetch_directory().$segments[0]);
				}
			}
			else
			{
				$this->set_class($this->default_controller);
				$this->set_method('');
			
				if ( ! file_exists(PATH_BASE.'controllers'.DS.$this->fetch_directory().$this->default_controller.EXT))
				{
					$this->directory = '';
					return array();
				}
			
			}
			return $segments;
		}
		show_404($segments[0]);
	}
	
	/**
	 * 设置控制类名
	 *
	 * @访问权限	public
	 * @参数		string
	 * @返回		void
	 */	
	function set_class($class)
	{
		$this->class = $class;
	}
	
	/**
	 * 返回当前控制类
	 *
	 * @访问权限	public
	 * @返回		string
	 */	
	function fetch_class()
	{
		return $this->class;
	}
	
	/**
	 *  设置动作函数方法名
	 *
	 * @访问权限	public
	 * @参数		string
	 * @返回		void
	 */	
	function set_method($method)
	{
		$this->method = $method;
	}
	
	/**
	 *  获取当前动作函数方法
	 *
	 * @访问权限	public
	 * @返回		string
	 */	
	function fetch_method()
	{
		if ($this->method == $this->fetch_class())
		{
			return '';
		}
		return $this->method;
	}
	
	/**
	 *  设置路径
	 *
	 * @访问权限	public
	 * @参数		string
	 * @返回		void
	 */	
	function set_directory($dir)
	{
		$this->directory = $dir.DS;
	}
	/**
	 *  获取路径
	 *
	 * @访问权限	public
	 * @返回		string
	 */	
	function fetch_directory()
	{
		return $this->directory;
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>